//
//  MusicErchoViewController.m
//  ManSavour
//
//  Created by dllo on 15/10/6.
//  Copyright (c) 2015年 刘志强. All rights reserved.
//

#import "MusicErchoViewController.h"
#import "MusicErchModel.h"
#import "MusicErchTableViewCell.h"
#import "MusicAllDetailViewController.h"
#import "BaseCollectionView.h"
@interface MusicErchoViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>


@property (nonatomic,retain)BaseCollectionView *mycollectionView;
@property (nonatomic,retain)NSMutableArray *array;

@property (nonatomic,assign)BOOL isUpLoading;
@property (nonatomic,assign)NSInteger currentPage;
@property (nonatomic,copy)NSString *modelPath;
@end

@implementation MusicErchoViewController



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    //导航栏透明，去除底部黑线
    
    
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    
    navigationBar.translucent = NO;
    
    /******************导航栏***************/

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSArray *arrayFile = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    self.modelPath = [arrayFile lastObject];
    
    
    //NSLog(@"%@",self.modelPath);
     [self setAutomaticallyAdjustsScrollViewInsets:NO];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    flow.itemSize = CGSizeMake(375 *XWIDTH, 200 *XHIGHT);

    self.mycollectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT) collectionViewLayout:flow];
    self.mycollectionView.delegate = self;
    self.mycollectionView.dataSource = self;
    
    [self.mycollectionView registerClass:[MusicErchTableViewCell class] forCellWithReuseIdentifier:@"qiang"];

    self.array = [NSKeyedUnarchiver unarchiveObjectWithFile:[self.modelPath stringByAppendingPathComponent:@"model.plist"]];
    
    if (self.array == nil) {
        self.array =[NSMutableArray array];
    }
   
    [self addHeaderRefresh];
    [self addFooterRefresh];
    
    [self.view addSubview:self.mycollectionView];

    
}

- (void)addHeaderRefresh
{
    
    
    self.mycollectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading =NO;
        //    self.array = [NSMutableArray array];
        
        
        self.currentPage = 1;
        [self getAfn:self.currentPage];

        
    }];
  
    [self.mycollectionView.header beginRefreshing];
}

- (void)addFooterRefresh
{
    
    self.mycollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.isUpLoading =YES;
        //NSLog(@"上拉");
        //上拉一次页码加1
        self.currentPage ++;
        [self getAfn:self.currentPage];

        
    }];
}


- (void)getAfn:(NSInteger)page
{
    
    NSString *urlste = [NSString stringWithFormat:@"http://echosystem.kibey.com/channel/get?with_sound=0&order=hot&page=%ld",page];
    

    
    
    [AFNTools getAFN:urlste  andpara:nil andBlock:^(id result) {
        NSDictionary *allDic = result;
        MusicErchModel *model = [[MusicErchModel alloc]init];
        [model setValuesForKeysWithDictionary:allDic];
        
        model.dicResult = [allDic objectForKey:@"result"];
        
        if(self.isUpLoading == NO)
        {
            //如果是下拉操作 清空数组
            [self.array removeAllObjects];
            
        }
        
        
        for (MusicErchData *m in model.result.data) {
            [self.array addObject:m];
        }
        //NSLog(@"%ld",self.array.count);
        BOOL result1 = [NSKeyedArchiver archiveRootObject:self.array toFile:[self.modelPath stringByAppendingPathComponent:@"model.plist"]];
        
        if (result1) {
            //NSLog(@"++归档成功");
        }else
        {
            //NSLog(@"归档失败");
        }
        

    [self.mycollectionView reloadData];
        
        [self.mycollectionView.header endRefreshing];
        [self.mycollectionView.footer endRefreshing];
   
    } andfailBlock:^(id fail) {
        [self.mycollectionView.header endRefreshing];
        [self.mycollectionView.footer endRefreshing];

        
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return self.array.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *colli = @"qiang";
    MusicErchTableViewCell *coll = [collectionView dequeueReusableCellWithReuseIdentifier:colli forIndexPath:indexPath];
    
 
    MusicErchData *model =[self.array objectAtIndex:indexPath.row];
    
    coll.model = model;
    return coll;
}





- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MusicAllDetailViewController *deatil = [[MusicAllDetailViewController alloc]init];
    MusicErchData *mdeol =[self.array objectAtIndex:indexPath.row];
    deatil.id = mdeol.id;
    deatil.urlstr = mdeol.pic;
    deatil.navigationItem.title = mdeol.name;
    [SHAREDAPPDELE.myTB isHiddenTabBar:YES withViewController:deatil];
    [self.navigationController pushViewController:deatil animated:YES];
  
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
